//
//  BSLAlbumsTableViewController.m
//  Demo
//
//  Created by RYAN on 15/6/23.
//
//

#import "BSLAlbumsTableViewController.h"
#import "BSLAlbumsTableViewCell.h"
#import "BSLPhotosViewController.h"
#import "BSLGalleryPlugin.h"

#define BSL_ALBUM_CELL_INDENTIFIER @"BSL_ALBUM_CELL_INDENTIFIER"

@interface BSLAlbumsTableViewController ()

@property (nonatomic, strong) NSMutableArray *albums;
@property (nonatomic, strong) ALAssetsLibrary *assetsLibrary;

@end

@implementation BSLAlbumsTableViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self setupNav];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"BSLAlbumsTableViewCell" bundle:nil] forCellReuseIdentifier:BSL_ALBUM_CELL_INDENTIFIER];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self loadAlbums];
}

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setToolbarHidden:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)setupNav{
    self.title = [[BSLGalleryHelper shareInatance] titleFromFilter];
    
    UIBarButtonItem *rightBtn = [[UIBarButtonItem alloc]
                                 initWithTitle:@"取消"
                                 style:UIBarButtonItemStylePlain
                                 target:self
                                 action:@selector(cancel)];
    self.navigationItem.rightBarButtonItem = rightBtn;
}

- (void)cancel{
    
    [(GalleryNavigationController *)self.navigationController dismiss:nil errorMessage:@"select no images"];
}

#pragma mark - data for UITableView
//加载本地相薄
- (void)loadAlbums{
    NSMutableArray *groupArray;
    _assetsLibrary = [[ALAssetsLibrary alloc] init];
    groupArray = [[NSMutableArray alloc] initWithCapacity:1];
    
    [self.assetsLibrary enumerateGroupsWithTypes:ALAssetsGroupAll usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
        if (group) {
            
            [group setAssetsFilter:[[BSLGalleryHelper shareInatance] groupFilter]];
            
            if ([group numberOfAssets] != 0) {
                [groupArray addObject:group];
            }
        }
        else{
            self.albums = groupArray;
            if (self.albums.count == 0) {
                
                UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"相册没有相关资源" delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];
                [alert show];
            }
            
            else{
                
                [self.tableView reloadData];
            }
        }
        
    } failureBlock:^(NSError *error) {
        _albums = nil;
        NSLog(@"Group not found!\n");
    }];
    
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    // Return the number of sections.
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    return [self.albums count];
}


- (BSLAlbumsTableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    BSLAlbumsTableViewCell *cell = (BSLAlbumsTableViewCell *)[tableView dequeueReusableCellWithIdentifier:BSL_ALBUM_CELL_INDENTIFIER forIndexPath:indexPath];
    
    // Configure the cell...
    ALAssetsGroup *album = [self.albums objectAtIndex:indexPath.row];
    [cell setDataWithAlbum:album];
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    CGFloat height = CELL_HEIGHT;
    return height;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ALAssetsGroup *group = [self.albums objectAtIndex:indexPath.row];
    BSLPhotosViewController *controller = [[BSLPhotosViewController alloc] initWithGroup:group];
    [self.navigationController pushViewController:controller animated:[BSLGalleryHelper shareInatance].isAnimated];
}

/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/

/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
    } else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/

/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/

/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    if (buttonIndex == alertView.cancelButtonIndex) {
        [(GalleryNavigationController *)self.navigationController dismiss:nil errorMessage:@"no image or no video"];
    }
}

@end
